<!-- subpkg_archive/list/index.vue -->
<script setup>
  import { ref } from 'vue'
  import { onShow } from '@dcloudio/uni-app'
  import { patientListApi, removePatientApi } from '@/services/patinet.js'
  // 是否显示页面内容
  const pageshow = ref(false)
  //患者列表
  const patinetList = ref([])
  // 侧滑按钮配置
  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
      },
    },
  ])
  //
  onShow(() => {
    getPatinetList()
  })
  const getPatinetList = async () => {
    const res = await patientListApi()
    //渲染列表数据
    patinetList.value = res.data
    // 展示页面
    pageshow.value = true
  }

  const onSwipeActionClick = async (id, i) => {
    const res = await removePatientApi(id)
    uni.utils.toast('删除成功')
    // Vue 实例中的数据也要同步删除
    patinetList.value.splice(i, 1)
  }
</script>

<template>
  <scroll-page v-if="pageshow">
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item
          :right-options="swipeOptions"
          v-for="(v, i) in patinetList"
          :key="v.id + '_' + v.defaultFlag"
          @click="onSwipeActionClick(v.id, i)"
        >
          <view class="archive-card" :class="{ active: v.defaultFlag === 1 }">
            <view class="archive-info">
              <text class="name">{{ v.name }}</text>
              <text class="id-card">
                {{ v.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2') }}</text
              >
              <text class="default" v-if="v.defaultFlag === 1">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ v.genderValue }}</text>
              <text class="age">{{ v.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/index?id=${v.id}`"
            >
              <uni-icons
                type="icon-edit"
                size="20"
                color="#16C2A3"
                custom-prefix="iconfont"
              />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patinetList.length < 6" class="archive-card">
        <navigator
          class="add-link"
          hover-class="none"
          url="/subpkg_archive/form/index"
        >
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
    </view>
  </scroll-page>
</template>

<style lang="scss">
  @import './index.scss';
</style>
